package com.example.administrator.myapplication;


import android.Manifest;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import pub.devrel.easypermissions.EasyPermissions;


public class MainActivity extends AppCompatActivity {
    public static final int WORDS_PER_PAGE=300;//每页的字符个数
    TextView tvShow;
    int page=0;//页码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态申请使用sd卡的权限
        String[]perms={Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if(!EasyPermissions.hasPermissions(this,perms)){
            EasyPermissions.requestPermissions(this," write sd",1,perms);
        }



        //找到控件
        Button btnOpen= (Button) findViewById(R.id.btn_open);
        Button btnUp= (Button) findViewById(R.id.btn_up);
        Button btnDown= (Button) findViewById(R.id.btn_down);
        tvShow= (TextView) findViewById(R.id.tv_show);
        btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击按钮，显示下一页
                page++;//页码+1

                String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/note.txt";
                File file=new File(path);
                try {
                    FileInputStream in = new FileInputStream(file);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
                    //读一页，一页多少个字符300个
                    String data = "";//
                    //想要显示第10页，将前9页的字符（WORDS_PER_PAGE*（10-1））跳过去，从第十页开始显示
                    br.skip(WORDS_PER_PAGE*(page-1));
                    //读当前页面的字符
                    for(int i=0;i<WORDS_PER_PAGE;i++){
                        int c=br.read();//读一个字符
                        if(c==-1){
                            break;//文件结束符号=-1
                        }
                        data=data+(char)c;

                    }
                    //显示
                    tvShow.setText(data);

                    br.close();

                }catch (Exception e){

                }

            }
        });
        //
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击按钮，打开文件
                //读文件 1-封装file 2-输入流关联file 3-缓冲区-定义一个数组 4-读将文件输入流中的数据读入到缓冲区5-关闭
                //String path="storage/emulated/0/Download/note.txt";//文件路径和文件名note.txt
                String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/note.txt";
                File file=new File(path);
                page=1;
                try {
                    FileInputStream in=new FileInputStream(file);
                    BufferedReader br=new BufferedReader(new InputStreamReader(in,"utf-8"));
                    //读一页，一页多少个字符300个
                    String data="";//
                    for(int i=0;i<WORDS_PER_PAGE;i++){
                       int c=br.read();//读一个字符
                        if(c==-1){
                            break;//文件结束符号=-1
                        }
                        data=data+(char)c;

                    }
                    //显示
                    tvShow.setText(data);

                    br.close();



                } catch (Exception e) {
                    e.printStackTrace();
                }

                //将文件的一部分内容(一页)显示在文本框
            }
        });
    }
}
